package artid.com.arttogo.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

import java.util.Timer;
import java.util.TimerTask;

import artid.com.arttogo.utils.ScanningDeviceUtils;

/**
 * 
 *    
 * 项目名称：BLE_IBeacon   
 * 类名称   ：BluetoothServiceTwo   
 * 类描述   ：蓝牙服务2   
 * 创建人   ：耿卫斌   
 * 创建时间：2016-9-14 上午10:11:50   
 *
 */
public class BluetoothServiceTwo extends Service {
	private Context mContext;
	private Boolean isClose=false;
	public final static String TAG = "com.service.BluetoothServiceTwo";

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		//		Log.e("log","BluetoothServiceTwo已进入");  
		new Thread(mRunnable).start();
		return START_REDELIVER_INTENT;  
	}  

	Runnable mRunnable = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			Timer timer = new Timer();
			TimerTask task = new TimerTask() {

				@Override
				public void run() {  
					//Log.e(TAG, "ServiceTwo Run: " + System.currentTimeMillis());  
					mContext=BluetoothServiceTwo.this;
					if(!isClose){
						ScanningDeviceUtils.openBluetooth(mContext);
						boolean b = DeviceScanActivity.isServiceWorked(BluetoothServiceTwo.this, "com.aicpsp.artworkartc.bluetooth.BluetoothServiceOne");  
						//					Log.d("log", "BluetoothServiceOne是否启动："+b);
						if(!b) {  
							Intent service = new Intent(BluetoothServiceTwo.this, BluetoothServiceOne.class);
							startService(service);  
						}  
					}
				}  
			};  
			timer.schedule(task, 0, 10000);  
		}
	};

	public void onDestroy() {
		isClose=true;
	};

	@Override
	public IBinder onBind(Intent arg0) {
		return null;  
	} 
}